package app.foodnote.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class FoodNotesDataSource {
	

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns =  {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.FOOD_NAME, MySQLiteHelper.ADDRESS,
			MySQLiteHelper.CITIES, MySQLiteHelper.PHONE_NUMBER, MySQLiteHelper.OTHER_NOTES, 
			MySQLiteHelper.LONGITUDE, MySQLiteHelper.LATITUDE
			};
	public FoodNotesDataSource(Context context){
		dbHelper = new MySQLiteHelper(context);
	}
	public void openToWrite() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	public void openToRead() throws SQLException {
		database = dbHelper.getReadableDatabase();
	}
	public void close(){
		dbHelper.close();
	}
	public void onUpgrade(int oldVersion, int newVersion){
		dbHelper.onUpgrade(database, oldVersion, newVersion);
	}
	public FoodNote addFoodNotes(FoodNote foodNote){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.FOOD_NAME, foodNote.getFoodName());
		values.put(MySQLiteHelper.ADDRESS, foodNote.getAddress());
		values.put(MySQLiteHelper.CITIES, foodNote.getCities());
		values.put(MySQLiteHelper.PHONE_NUMBER, foodNote.getPhoneNumber());
		values.put(MySQLiteHelper.OTHER_NOTES, foodNote.getOtherNotes());
		values.put(MySQLiteHelper.LONGITUDE, foodNote.getLongitude());
		values.put(MySQLiteHelper.LATITUDE, foodNote.getLatitude());
		long insertId = database.insert(MySQLiteHelper.TABLE_FOODNOTE, null, values);
		Cursor cursor = database.query(MySQLiteHelper.TABLE_FOODNOTE, allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		FoodNote newFoodNote = cursorToFoodNote(cursor);
		cursor.close();
		return newFoodNote;
	}
	private FoodNote cursorToFoodNote(Cursor cursor) {
		FoodNote foodnote = new FoodNote();
		foodnote.setId(cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID)));
		foodnote.setFoodName(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.FOOD_NAME)));
		foodnote.setAddress(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.ADDRESS)));
		foodnote.setCities(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.CITIES)));
		foodnote.setPhoneNumber(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.PHONE_NUMBER)));
		foodnote.setOtherNotes(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.OTHER_NOTES)));
		foodnote.setLongitude(cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.LONGITUDE)));
		foodnote.setLatitude(cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.LATITUDE)));
		return foodnote;
	}
	public void deleteFoodNote(FoodNote foodNote){
		database.delete(MySQLiteHelper.TABLE_FOODNOTE, MySQLiteHelper.COLUMN_ID + "=?", new String[] {String.valueOf(foodNote.getId())});
	}
	
	public int updateFoodNote(FoodNote foodNote){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.FOOD_NAME, foodNote.getFoodName());
		values.put(MySQLiteHelper.ADDRESS, foodNote.getAddress());
		values.put(MySQLiteHelper.CITIES, foodNote.getCities());
		values.put(MySQLiteHelper.PHONE_NUMBER, foodNote.getPhoneNumber());
		values.put(MySQLiteHelper.OTHER_NOTES, foodNote.getOtherNotes());
		values.put(MySQLiteHelper.LONGITUDE, foodNote.getLongitude());
		values.put(MySQLiteHelper.LATITUDE, foodNote.getLatitude());
		
		return database.update(MySQLiteHelper.TABLE_FOODNOTE, values, MySQLiteHelper.COLUMN_ID + "=?", new String[] {String.valueOf(foodNote.getId())});
	}
	
	public List<FoodNote> getAllFoodNotes(){
		List<FoodNote> foodnotes = new ArrayList<FoodNote>();
		Cursor cursor = database.query(MySQLiteHelper.TABLE_FOODNOTE, allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			FoodNote foodnote = cursorToFoodNote(cursor);
			foodnotes.add(foodnote);
			cursor.moveToNext();
		}
		cursor.close();
		return foodnotes;
	}
	public Cursor getAllEntry(){
		Cursor cursor = database.query(MySQLiteHelper.TABLE_FOODNOTE, allColumns, null, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
		return cursor;
	}
	
}
